package com.gwcloud.leetcode.hot100;

import java.util.LinkedList;

/** 
* @ClassName   : Leetcode0394 
* @author      : ganwei 
* @date        : 2020年5月23日 
* @Description : TODO 
*/
public class Leetcode0394 {
    /**
     * -解题思路，栈
     */
	public String decodeString(String s) {
    	StringBuilder res = new StringBuilder();
    	int multi = 0;
    	LinkedList<Integer> stack_multi = new LinkedList<Integer>();
    	LinkedList<String> stack_res = new LinkedList<String>();
    	
    	for(Character c : s.toCharArray()) {
    		if(c == '[') {
    			stack_multi.addLast(multi);
    			stack_res.addLast(res.toString());
    			multi = 0;
    			res = new StringBuilder();
    		}else if(c == ']') {
    			StringBuilder tmp = new StringBuilder();
    			int cur_multi = stack_multi.removeLast();
    			for(int i = 0; i < cur_multi; i++) {
    				tmp.append(res);
    			}
    			res = new StringBuilder(stack_res.removeLast() + tmp);
    		}else if(c >= '0' && c <= '9') {
    			multi = multi * 10 + Integer.parseInt(c+"");
    		}else {
    			res.append(c);
    		}
    	}
    	
    	return res.toString();
    }
    
    
    /**
     * -主函数
     */
    public static void main(String[] args) {
    	
    }
}
